<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>添加学生</title>
    <link rel="stylesheet" type="text/css"
          href="assets/admin/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="assets/admin/css/admin.css"/>
</head>

<body>
<div class="page-content-wrap" id="studentAdd">

    <form class="layui-form" action="" id="">
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">学生学号</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input type="text" name="studentNo" lay-verify="studentNo"-->
<!--                       autocomplete="off" placeholder="请输入学号" class="layui-input">-->
<!--            </div>-->
<!--        </div>-->
        <input type="hidden" name="subjectId" lay-verify="subjectId"
               autocomplete="off" placeholder="请输入学科ID" class="layui-input">
        <div class="layui-form-item">
            <label class="layui-form-label">学科名称</label>
            <div class="layui-input-block">
                <input type="text" name="subjectName" lay-verify="subjectName"
                       placeholder="请输入学科名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input type="text" name="description" lay-verify="description"
                       placeholder="请输入学科描述" autocomplete="off" class="layui-input" required>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">学时</label>
            <div class="layui-input-block">
                <input type="text" name="hours" lay-verify="hours"
                       placeholder="请输入学时" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item m-login-btn" style="text-align:center">
            <div class="layui-inline">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="addStudent">添加信息</button>
            </div>
            <div class="layui-inline">
                <button type="reset" class="layui-btn layui-btn-primary  site-demo-active"
                        data-type="reset">取消添加
                </button>
            </div>
        </div>
    </form>

    <hr></hr>
    <h5 style="text-align: center;">@学生成绩管理系统</h5>
</div>
<script src="assets/admin/layui/layui.all.js" type="text/javascript"
        charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer, layedit = layui.layedit;
        var $ = layui.jquery;
        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            subjectName: [
                /^[\S]{2,10}$/
                , '学科名称必须2到12位，且不能出现空格'
            ],
            description: [
                /^[\S]{2,50}$/
                , '学科描述必须2到50位，且不能出现空格'
            ],
            hours: [
                /^[1-9][0-9]{0,2}$/,
                '学时必须为1到999之间的数字'
            ],
            content: function (value) {
                layedit.sync(editIndex);
            }
        });
        form.on('submit(addStudent)', function (data) {
            var str = JSON.stringify(data.field)
            var param = JSON.parse(str);
            $.ajax({
                //几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "/subject/addSubject",//url
                data: param,
                success: function (result) {
                    if (result.code === "0") {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                        parent.layer.msg('添加学科成功');
                        parent.layui.table.reload('idTest', {page: {curr: 1}});
                    } else {
                        layer.msg('服务器错误', {
                            icon: 5
                        });
                    }
                },
                error: function () {
                    layer.msg('服务器错误', {
                        icon: 5
                    });
                }
            });

            return false;
        });
        //触发事件
        var active = {
            reset: function () {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
                parent.layer.msg('已取消操作');
            }
        };
        $('#studentAdd .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>

</html>